package com.heyige.controller;

import javax.validation.Valid;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heyige.common.CommResponse;
import com.heyige.dto.UserDto;
import com.heyige.dto.request.BindMiniProgramRequest;
import com.heyige.dto.request.BindWechatRequest;
import com.heyige.dto.request.ChangePasswordRequest;
import com.heyige.entity.User;
import com.heyige.facade.UserFacade;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("api/users")
@RequiredArgsConstructor
@Tag(name = "用户管理", description = "用户相关接口")
public class UserController {

    private final UserFacade userFacade;

    @PostMapping("login")
    @Operation(summary = "用户登录", description = "用户通过用户名和密码登录系统")
    public CommResponse<UserDto.LoginResponse> login(
            @Parameter(description = "登录请求参数") @Valid @RequestBody UserDto.LoginRequest loginRequest) {
        UserDto.LoginResponse response = userFacade.login(loginRequest);
        return CommResponse.ok(response);
    }

    @PostMapping("register")
    @Operation(summary = "用户注册", description = "新用户注册")
    public CommResponse<User> register(
            @Parameter(description = "注册请求参数") @Valid @RequestBody UserDto.RegisterRequest registerRequest) {
        User user = userFacade.register(registerRequest);
        return CommResponse.ok(user);
    }

    @GetMapping("{userId}")
    @Operation(summary = "根据ID获取用户", description = "根据用户ID获取用户信息")
    public CommResponse<User> getUserById(@Parameter(description = "用户ID", required = true, example = "1") @PathVariable Long userId) {
        User user = userFacade.getUserById(userId);
        return CommResponse.ok(user);
    }

    @GetMapping("username/{username}")
    @Operation(summary = "根据用户名获取用户", description = "根据用户名获取用户信息")
    public CommResponse<User> getUserByUsername(
            @Parameter(description = "用户名", required = true, example = "admin") @PathVariable String username) {
        User user = userFacade.getUserByUsername(username);
        return CommResponse.ok(user);
    }

    @GetMapping
    @Operation(summary = "获取用户列表", description = "分页获取用户列表")
    public CommResponse<IPage<User>> getUserList(
            @Parameter(description = "页码", example = "0") @RequestParam(defaultValue = "0") Integer page,
            @Parameter(description = "每页大小", example = "20") @RequestParam(defaultValue = "20") Integer size) {
        IPage<User> users = userFacade.getUserList(page, size);
        return CommResponse.ok(users);
    }

    @PutMapping("{userId}")
    @Operation(summary = "更新用户信息", description = "更新指定用户的信息")
    public CommResponse<User> updateUser(
            @Parameter(description = "用户ID", required = true, example = "1") @PathVariable Long userId,
            @Parameter(description = "用户信息") @RequestBody UserDto userDto) {
        User user = userFacade.updateUser(userId, userDto);
        return CommResponse.ok(user);
    }

    @PostMapping("{userId}/changePassword")
    @Operation(summary = "修改密码", description = "修改指定用户的密码")
    public CommResponse<Void> changePassword(
            @Parameter(description = "用户ID", required = true, example = "1") @PathVariable Long userId,
            @Parameter(description = "修改密码请求参数") @RequestBody ChangePasswordRequest request) {
        userFacade.changePassword(userId, request.getOldPassword(), request.getNewPassword());
        return CommResponse.ok();
    }

    @PostMapping("{userId}/bindWechat")
    @Operation(summary = "绑定微信", description = "为用户绑定微信账号")
    public CommResponse<Void> bindWechat(
            @Parameter(description = "用户ID", required = true, example = "1") @PathVariable Long userId,
            @Parameter(description = "绑定微信请求参数") @RequestBody BindWechatRequest request) {
        userFacade.bindWechat(userId, request.getWxOpenid(), request.getWxUnionid());
        return CommResponse.ok();
    }

    @PostMapping("{userId}/bindMp")
    @Operation(summary = "绑定小程序", description = "为用户绑定微信小程序")
    public CommResponse<Void> bindMiniProgram(
            @Parameter(description = "用户ID", required = true, example = "1") @PathVariable Long userId,
            @Parameter(description = "绑定小程序请求参数") @RequestBody BindMiniProgramRequest request) {
        userFacade.bindMiniProgram(userId, request.getMiniProgramOpenid());
        return CommResponse.ok();
    }

    @PostMapping("logout")
    @Operation(summary = "用户退出登录", description = "用户退出登录系统")
    public CommResponse<Void> logout(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (token != null && token.startsWith("Bearer ")) {
            token = token.substring(7);
            userFacade.logoutUser(token);
        }
        return CommResponse.ok();
    }

}